home *** CD-ROM | disk | FTP | other *** search
- { ManyWind TransSkel demonstration}
-
- { This application allows up to twenty windows to be created at once,}
- { with the New item under the File menu. The name of each window}
- { appears under the Windows menu (which is not created until at least}
- { one window exists). Selecting the window name from the Windows menu}
- { brings the window to the front. For every window created, Skel is}
- { told to create a new handler. If the window's close box is clicked,}
- { the handler removes the window name from the Windows menu, disposes}
- { of the window, and removes itself from the window handler list. If}
- { the window was the last window, the Windows menu handler removes}
- { itself from the menu handler list.}
-
- { When the first window is created, a Color menu also appears. This}
- { allows the color of the content region of the frontmost window to}
- { be changed. It goes away when the last window is closed.}
-
- { To quit, select Quit from the File menu or type command-Q.}
-
- { ManyWind demonstrates dynamic window and menu creation and disposal.}
- { It also shows how handler procedures may be shared among handlers}
- { for different windows.}
-
- { The project should include this file, TransSkel.c (or a project}
- { built from TransSkel.c), and MacTraps.}
-
- { 28 June 1986 Paul DuBois}
- { 7 January 1987 Owen Hartnett, Ωhm Software Co. }
-
- PROGRAM ManyWind;
-
- USES
- TransSkelPas;
-
- CONST
- maxWind = 20; { maximum number of windows existing at once }
-
- { menu numbers }
-
- aMenuNum = 1; { Apple menu }
- fMenuNum = 2; { File menu }
- wMenuNum = 3; { Windows menu }
- cMenuNum = 4; { Color menu }
-
- { File menu item numbers }
-
- new = 1;
- quit = 3;
-
- { Color menu items numbers }
-
- cWhite = 1;
- cLtGray = 2;
- cGray = 3;
- cDkGray = 4;
- cBlack = 5;
-
- VAR
- fileMenu, windowMenu, colorMenu : MenuHandle;
-
- windCount : integer; { number of currently existing windows }
- windNum : longint; { id of last window created }
-
- PROCEDURE MakeWindow;
- forward;
- PROCEDURE DoWClose;
- forward;
-
- PROCEDURE DoWUpdate;
-
- VAR
- thePort : GrafPtr;
-
- BEGIN
- GetPort(thePort);
- EraseRect(thePort^.portRect); { repaint w/background pattern }
- END;
-
- PROCEDURE DoMClobber (theMenu : MenuHandle);
-
- BEGIN
- DisposeMenu(theMenu);
- END;
-
- PROCEDURE DoFileMenu (item : integer);
-
- BEGIN
- CASE item OF
- quit :
- SkelWhoa; { tell SkelMain to quit }
- new :
- MakeWindow; { make a new window }
- END;
- END;
-
- { Dispose of window. Skel makes sure the port is pointing to the}
- { appropriate window, so this procedure can determine which window}
- { is to be disposed, of without being told explicitly.}
-
-
- PROCEDURE DoWClobber;
-
- VAR
- thePort : GrafPtr;
-
- BEGIN
- GetPort(thePort); { grafport of window to dispose of }
- DisposeWindow(WindowPtr(thePort));
- END;
-
- { Change the background pattern of the frontmost window. Ignore}
- { if the front window is a DA window.}
-
- PROCEDURE DoColorMenu (item : integer);
-
- VAR
- w : WindowPeek;
- w2 : WindowPtr;
-
- BEGIN
- w := WindowPeek(FrontWindow);
- SetPort(WindowPtr(w)); {*** Fixed bug in original windows }
- IF w^.windowKind >= 0 THEN { front is not DA window }
- BEGIN
- CASE item OF
- cWhite :
- BackPat(white);
- cLtGray :
- BackPat(ltGray);
- cGray :
- BackPat(gray);
- cDkGray :
- BackPat(dkGray);
- cBlack :
- BackPat(black);
- END;
- w2 := WindowPtr(w);
- EraseRect(w2^.portRect);
- END;
- END;
-
-
- PROCEDURE DoWindowMenu (item : integer);
-
- VAR
- iTitle, wTitle : Str255;
- w : WindowPeek;
-
- BEGIN
- GetItem(windowMenu, item, iTitle); { get window name }
- w := WindowPeek(FrontWindow);
- WHILE w <> NIL DO
- BEGIN
- GetWTitle(WindowPtr(w), wTitle);
- IF EqualString(iTitle, wTitle, false, true) THEN
- BEGIN
- SelectWindow(WindowPtr(w));
- w := NIL;
- END;
- IF w <> NIL THEN
- w := w^.nextWindow;
- END;
- END;
-
- { Make new window. Locate at (100, 100) if no other windows, else}
- { offset slightly from front window. The window title is the next}
- { window number (1, 2, 3, ...). If this is the first window, create}
- { the Windows and Color menus. Add the window title as the last item}
- { of the Windows menu.}
-
- { If the maximum window count has been reached, disable New in the}
- { File menu.}
-
- PROCEDURE MakeWindow;
-
- VAR
- w : WindowPtr;
- r, r2 : Rect;
- s : Str255;
-
- BEGIN
- SetRect(r, 0, 0, 200, 150);
- w := FrontWindow;
- IF w = NIL THEN
- OffsetRect(r, 100, 100)
- ELSE
- BEGIN
- r2 := w^.portBits.bounds;
- OffSetRect(r, 20 - r2.left, 20 - r2.top);
- IF (r.left > 480) OR (r.top > 300) THEN { keep on screen }
- OffsetRect(r, 40 - r.left, 40 - r.top);
- END;
- WindNum := windnum + 1;
- NumToString(windNum, s);
- w := NewWindow(NIL, r, s, true, documentProc, WindowPtr(-1), true, 0);
- SkelWindow(w, NIL, NIL, @DoWUpdate, NIL, @DoWClose, @DoWclobber, NIL, false);
- windCount := windCount + 1;
- IF windCount - 1 = 0 THEN { if first window, create new menus }
- BEGIN
- colorMenu := NewMenu(cMenuNum, 'Color');
- AppendMenu(colorMenu, 'White;Light Gray;Gray; Dark Gray; Black');
- SkelMenu(colorMenu, @DoColorMenu, @DoMClobber);
- windowMenu := NewMenu(wMenuNum, 'Windows');
- SkelMenu(windowMenu, @DoWindowMenu, @DoMClobber);
- END;
- AppendMenu(windowMenu, s);
- IF windCount = maxWind THEN
- DisableItem(fileMenu, new);
- END;
-
- { Mouse was clicked in close box. Remove the window handler (which}
- { causes the window to be disposed of), and delete the window title}
- { from the Windows menu. If the window was the last one, delete the}
- { Windows and Color menus entirely.}
-
- { Skel makes sure the port is pointing to the appropriate window, so}
- { this procedure can determine which window had its close box clicked,}
- { without being told explicitly.}
-
- PROCEDURE DoWClose;
-
- VAR
- thePort : GrafPtr;
- m : MenuHandle;
- i, mItems : integer;
- iTitle, wTitle : Str255;
-
- BEGIN
- GetPort(thePort); { grafport of window to be closed }
- GetWTitle(WindowPtr(thePort), wTitle);
- SkelRmveWind(WindowPtr(thePort));
- windCount := windCount - 1;
- IF windCount = 0 THEN
- BEGIN
- SkelRmveMenu(windowMenu); { last window - clobber menus }
- SkelRmveMenu(colorMenu);
- END
- ELSE
- BEGIN { just take out of menu }
- m := NewMenu(wMenuNum, 'Windows');
- mItems := CountMItems(windowMenu);
- FOR i := 1 TO mItems DO
- BEGIN
- GetItem(windowMenu, i, iTitle);
- IF NOT EqualString(iTitle, wTitle, false, true) THEN
- AppendMenu(m, iTitle);
- END;
- SkelRmveMenu(windowMenu); { remove old Windows menu }
- windowMenu := m; { and install new one }
- SkelMenu(windowMenu, @DoWindowMenu, @DoMClobber);
- END;
- EnableItem(fileMenu, new); { can always create at least one more now }
- END;
-
-
- BEGIN
-
- WindCount := 0;
- WindNum := 0;
- SkelInit; { initialize }
- SkelApple('', NIL); { handle desk accessories }
- fileMenu := NewMenu(fMenuNum, 'File'); { make File menu handler }
- AppendMenu(fileMenu, 'New/N;(-;Quit/Q');
- SkelGrowBounds(NIL, 50, 10, 500, 300);
- SkelMenu(fileMenu, @DoFileMenu, @DoMClobber);
- SkelMain; { loop 'til Quit selected }
- SkelClobber; { clean up }
- END.